package cn.weekimwee.firstlineofcodeforkotlin.unit2

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.view.View
import cn.weekimwee.firstlineofcodeforkotlin.BaseActivity
import cn.weekimwee.firstlineofcodeforkotlin.R
import kotlinx.android.synthetic.main.activity_first.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.startActivityForResult
import org.jetbrains.anko.toast

class FirstActivity : BaseActivity() {

    /**
     * 活动第一次创建
     */
    lateinit var TAG:String
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_first)
        TAG = this@FirstActivity::class.java.simpleName
        Log.e(TAG,"onCreate -> taskId : $taskId")
        savedInstanceState?.let {
            Log.e(TAG,"savedInstanceState:${it.getString("data_key")}")
        }
        button.setOnClickListener{ it.click() }
        button2.setOnClickListener { it.click() }
        button3.setOnClickListener { it.click() }
        button4.setOnClickListener { it.click() }
        button5.setOnClickListener { it.click() }
        button6.setOnClickListener { it.click() }
        button7.setOnClickListener { it.click() }
        button8.setOnClickListener { it.click() }
        button9.setOnClickListener { it.click() }
        button10.setOnClickListener { it.click() }
    }

    /**
     * 由不可见变为可见时调用
     */
    override fun onStart() {
        super.onStart()
        Log.e(TAG,"onStart")

    }

    /**
     * 已准备好和用户交互时调用。此时活动位于栈顶，处于运行状态
     */
    override fun onResume() {
        super.onResume()
        Log.e(TAG,"onResume")

    }

    /**
     * 在系统准备启用或恢复另一个活动时调用
     */
    override fun onPause() {
        super.onPause()
        Log.e(TAG,"onPause")

    }

    /**
     * activity完全不可见时调用
     */
    override fun onStop() {
        super.onStop()
        Log.e(TAG,"onStop")

    }

    /**
     * 销毁之前调用
     */
    override fun onDestroy() {
        super.onDestroy()
        Log.e(TAG,"onDestroy")

    }

    /**
     * activity从停止状态变为运行状态时调用
     */
    override fun onRestart() {
        super.onRestart()
        Log.e(TAG,"onRestart")
    }

    /**
     * 右上角创建菜单
     */
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.main,menu)
        return true
    }

    /**
     * 菜单选择监听
     */
    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        when(item?.itemId){
            R.id.add_item -> toast(resources.getString(R.string.first_activity_menu_add))
            R.id.remove_item -> toast(resources.getString(R.string.first_activity_menu_remove))
        }
        return true
    }

    private fun View.click(){
        when(this.id){
            R.id.button->{
                toast(resources.getString(R.string.first_activity_click_text))
                finish()
            }
            R.id.button2->{
                startActivityForResult<SecondActivity>(1,"second" to "Second-----Activity")
            }
            R.id.button3->{
                //隐式意图   需要在AndroidManifest中配置 action 和 category   好像category必须要配置一个DEFAULT
                startActivity(Intent("cn.weekimwee.firstlineofcodeforkotlin.third.ACTION_START").addCategory("cn.weekimwee.firstlineofcodeforkotlin.third.category"))
            }
            R.id.button4->{
                //隐式意图   需要在AndroidManifest中配置 action 和 category
                startActivity(Intent("cn.weekimwee.firstlineofcodeforkotlin.fourth.ACTION_START").addCategory("cn.weekimwee.firstlineofcodeforkotlin.fourth.category"))
            }
            R.id.button5->{
                //data:用于更精确的指定当前活动能够相应什么类型的数据
                startActivity(Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://www.qq.com")))
            }
            R.id.button6->{
                startActivity<DialogActivity>()
            }
            R.id.button7->{
                startActivity<StandardActivity>()
            }
            R.id.button8->{
                startActivity<SingleTaskActivity>()
            }
            R.id.button9->{
                startActivity<SingleTopActivity>()
            }
            R.id.button10->{
                startActivity<SingleInstanceActivity>()
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(resultCode!= Activity.RESULT_OK) return
        when(requestCode){
            1 -> text.text = data?.getStringExtra("return_data")
        }
    }

    override fun onSaveInstanceState(outState: Bundle?) {
        super.onSaveInstanceState(outState)
        outState?.putString("data_key","something u just typed")
    }
}
